﻿using System;
using Raven;
using UnityEngine;
using Vector2 = Raven.Vector2;

public class BoltDisplay : UnityDisplayObject
{
    private ProjectileBolt _projectileBolt;


    public BoltDisplay(BaseGameEntity entity) : base(entity)
    {
        _projectileBolt = entity as ProjectileBolt;
    }

    public override void Start()
    {
        base.Start();

        Vector2 pos = _owner.Pos();
        _gameObject.transform.position = new Vector3((float) pos.x, (float) pos.y);
        _gameObject.AddComponent<MeshFilter>().mesh =
            MeshFactory.LineMesh(Vector3.zero, Vector3.right * (float) _projectileBolt.MaxSpeed(), Color.green);
        MeshRenderer render = _gameObject.AddComponent<MeshRenderer>();
        render.material = new Material(Shader.Find("Sprites/Default"));
    }


    public override void Update()
    {
        _gameObject.SetActive(!_projectileBolt.isDead());
        if (!_projectileBolt.isDead())
        {
            Vector2 pos = _owner.Pos();
            _gameObject.transform.position = new Vector3((float) pos.x, (float) pos.y);

            Vector2 heading = _projectileBolt.Heading();
            Fix dot = Vector2.Dot(Vector2.right, heading);
            Fix rad = FixMath.Acos(dot) * (heading.y >= 0 ? 1 : -1);
            _gameObject.transform.rotation = Quaternion.AngleAxis((float) FixMath.RadsToDegs(rad), Vector3.forward);
        }
    }
}